#!/usr/bin/env python
#-*- coding: utf-8 -*-
from django.shortcuts import render_to_response,HttpResponse
from django.shortcuts import redirect
from django.template.context import RequestContext

from web import models
from models import UserInfo

# Create your views here.
'''
def Registeruser(request):
    registerForm = RegisterUser()

    if request.method == 'POST':
        data = RegisterUser(request.POST)
        if data.is_valid():
            dic_data = data.cleaned_data
            print dic_data
            UserInfo.objects.create(username=dic_data['username'],useremail=dic_data['useremail'],userphome=dic_data['userphome'],userpassword=dic_data['userpassword'])

        else:
            #data.errors.as_json()
            temp = data.errors.as_data()
            print temp['useremail'][0].messages[0]
    return render_to_response('register.html',{'data':registerForm},
                              context_instance=RequestContext(request))

def inqure_hosts(request,id):
   # id = int(id)
    obj = HostLists.objects.get(host_ip=id)
    #print obj.hostname
    #print type(obj)
    return render_to_response('selecthost.html',{'obj':obj},
                              context_instance=RequestContext(request))
'''


def register(request):
    '''
    t1 = models.UserType.objects.create(name='超级管理员')
    t2 = models.UserType.objects.create(name='普通管理员')

    s1 = models.UserType.objects.get(name='超级管理员')
    #print s1
    #print type(s1)  是一个对象
    #print s1.id

    u1 = models.UserInfo.objects.create(username='tom',password='123',email='tom@qq.com',user_type=s1)   # user_type是超级管理员或者是普通管理员
    s2 = models.UserType.objects.get(name='普通管理员')
    u2 = models.UserInfo.objects.create(username='jon',password='123',email='jon@qq.com',user_type=s2)
    groupobjA = models.UserGroup.objects.create(GroupName='group1')
    groupobjB = models.UserGroup.objects.create(GroupName='group2')
    groupobjA.user.add(u1)
'''
    return HttpResponse('注册成功')

def login(request):
    ret = {'status':''}
    if request.method == 'POST':
        username = request.POST.get('username',None)
        password = request.POST.get('password',None)
        print username
        print password
        is_empty = all([username,password])  # all()函数遍历整个列表，如果有一个值是false，则返回false，全部为true时则返回真。

        print is_empty
        if is_empty:
            count = UserInfo.objects.filter(username=username,password=password).count()
            print count
            if count ==1:
                return  redirect('/web/index/') # 要与urls.py中规则匹配
            else:
                ret['status'] = '用户名或密码错误'
        else:
            ret['status'] = '用户名密码不能为空'
    return render_to_response('login.html',ret,context_instance=RequestContext(request))

def index(request):
    return render_to_response('index.html',context_instance=RequestContext(request))

def host(request):
    ret = {'status':"",'data':None,'group':None}
    usergroup = models.UserGroup.objects.all()  # 取出所有组
    ret['group'] = usergroup
    #添加主机
    if request.method == 'POST':
        hostname = request.POST.get('hostname',None)
        ip = request.POST.get('ip',None)
        groupID = request.POST.get('group',None)
        # 验证用户输入是否为空
        is_empty = all([hostname,ip])
        if is_empty:
            groupObj = models.UserGroup.objects.get(id = groupID)   # 根据前端组的选择，获得groupID，根据groupID获取组名的对象--对象的类是UserGroup
            models.Asset.objects.create(hostname=hostname,ip = ip,user_group=groupObj)  # 将添加的主机信息写入数据库主机信息表,user_group传入的是一个对象，
        else:
            ret['status'] = 'hostname或ip不能为空'
    data = models.Asset.objects.all()   # 获取所有的主机信息
    ret['data'] = data
    #for i in data:
    #    print i.hostname
    '''
    obj = models.Asset.objects.filter(user_group__GroupName='group1')  # 联表查用双下划线,获取group1中的主机信息
    obj1 = models.Asset.objects.filter(user_group__id=2)    # 获取groupID=2的主机信息
    for item in obj1:
        print item.hostname
        print item.ip
        print item.user_group.id
        print item.user_group.GroupName
        print item   #是一行数据，是一个对象，封装了hostname、ip、user_group；user_group也是一个对象封装了id和GroupName
    '''

    return render_to_response('host.html',ret,context_instance=RequestContext(request)) # 将字典ret传给了模板
























